window.onload = function () {
	// let data = {
	// 	"token": 'fccd9b2a2984806dc23673ffa300221b',
	// 	"day_time": "2021 - 10 - 25",
	// 	"show_id":"1"
	// }

	if (sessionStorage.getItem('token') != null) {
		function getTime(dayTime, showName) {
			let nowTime = Date.now()
			let sumTime = `${dayTime} ${showName.slice(0, 5)}`
			let getPageTime = new Date(sumTime).getTime()
			console.log(getPageTime)
			console.log(nowTime)
			if (nowTime > getPageTime) {
				return '已约满'
			} else {
				return '可预约'
			}
		}
		let url = 'http://newshopapi.0melon0.cn/api/f_user/chooselist'
		let token = sessionStorage.getItem('token')
		url = `${url}?token=${token}`
		fetch(url, {
			method: 'GET',
		})
			.then((response) => {
				return response.json()
			})
			.then(function (data) {
				let arr = data.result.list
				let select = document.querySelector('.selectName')
				let frag = document.createDocumentFragment()
				arr.forEach(function (item, index) {
					let option = document.createElement('option')
					option.innerHTML = item.day_time
					option.value = item.day_time
					frag.appendChild(option)
				})
				select.appendChild(frag)
				// layui.form.render('select')
				select.addEventListener('change', function () {
					console.log(select.value)

					let url =
						'http://newshopapi.0melon0.cn/api/f_user/chooseday'
					url = `${url}?token=${token}&day_time=${select.value}`
					fetch(url, {
						method: 'GET',
					})
						.then(function (data) {
							return data.json()
						})
						.then(function (data) {
							let arr = data.result.list
							console.log(arr)
							let centerBox = document.querySelector('.centerBox')
							centerBox.innerHTML = ''
							arr.forEach(function (item, index) {
								console.log(item)
								let contentLine = document.createElement('div')
								contentLine.setAttribute(
									'data-day_time',
									item.day_time
								)
								contentLine.setAttribute(
									'data-show_name',
									item.show_name
								)
								contentLine.setAttribute(
									'data-show_id',
									item.show_id
								)
								contentLine.classList.add('contentLine')
								let div1 = document.createElement('div')
								let div2 = document.createElement('div')
								div1.classList.add('itemLine')
								div2.classList.add('itemLine')
								div2.classList.add('rember')
								div1.innerHTML = item.show_name
								div2.innerHTML = getTime(
									item.day_time,
									item.show_name
								)
								contentLine.appendChild(div1)
								contentLine.appendChild(div2)
								centerBox.appendChild(contentLine)
							})
							let contentLine = document.querySelectorAll(
								'.centerBox .contentLine'
							)
							console.log(contentLine)
							contentLine.forEach(function (item, index) {
								item.addEventListener('click', function () {
									console.log(this)
									let rember =
										this.querySelector('.rember').innerHTML
									if (rember === '可预约') {
										let url =
											'http://newshopapi.0melon0.cn/api/f_user/bookday'
										let data = {
											token: token,
											day_time:
												this.getAttribute(
													'data-day_time'
												),
											show_id:
												this.getAttribute(
													'data-show_Name'
												),
										}
										console.log(data)

										fetch(url, {
											method: 'POST',
											headers: {
												'Content-Type':
													'application/json',
											},
											body: JSON.stringify(data),
										})
											.then(function (response) {
												return response.json()
											})
											.then(function (data) {
												console.log(data)
												alert(data.msg)
												sessionStorage.setItem(
													'id',
													data.result.id
												)
												sessionStorage.setItem(
													'day_time',
													data.result.day_time
												)
												sessionStorage.setItem(
													'show_id',
													data.result.show_id
												)
												console.log(data.result.name)
												location.href =
													'../成功预约/index.html'
											})
											.catch(function (data) {
												alert(data.msg)
											})
									} else {
										alert('预约已满！')
									}
								})
							})
						})
				})
			})
			.catch(function (data) {
				alert(data.msg)
			})
	} else {
		alert('请先登录')
		location.href = '../index.html'
	}
}
